<template>
  <el-container class="homeContainer">
    <el-header>
      <div class="homeHeader">
        <h1>电商后台管理系统</h1>
      </div>
      <el-button
        type="info"
        @click="loginOut"
      >退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="istoggle ? '64px' : '200px'">
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#e43a77"
          unique-opened
          :collapse="istoggle"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <i
            class="el-icon-menu my-icon-menu"
            @click="cilltoggle"
          ></i>

          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuListData"
            :key="item.id"
          >
            <template slot="title">
              <!-- 图标 -->
              <i :class="icons[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavStatus('/' + subItem.path)"
            >
              <!-- 图标 -->
              <i class="el-icon-menu"></i>
              <!-- 文本 -->
              <span>{{ subItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuListData: [],
      icons: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '102': 'iconfont icon-shangpin',
        '101': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      //是否折叠
      istoggle: false,
      //被激活的链接状态
      activePath: ''
    }
  },
  // 挂载函数
  created() {
    this.getMenuList()
    // 获取本地中链接状态
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    loginOut() {
      // 清空token
      window.sessionStorage.clear()
      // 跳转到登录页
      this.$router.push('/login')
    },
    //发起请求获取左侧菜单数据
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      // console.dir(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.res)
      //将获得数据更新
      this.menuListData = res.data
    },
    cilltoggle() {
      this.istoggle = !this.istoggle
    },
    saveNavStatus(activePath) {
      // 将转态链接保存到本地中
      window.sessionStorage.setItem('activePath', activePath)
      // 设置链接状态
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.homeContainer {
  width: 100%;
  height: 100%;
}
.el-menu {
  border: none;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  .homeHeader {
    width: 200px;
    color: #eee;
    font-weight: bold;
    margin-left: 30px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .el-button {
    box-sizing: border-box;
  }
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.el-submenu i {
  margin-right: 15px;
}
.my-icon-menu {
  display: flex;
  justify-content: flex-end;
  padding: 5px;
  margin-right: 15px;
  font-size: 28px;
  color: #ccc;
  cursor: pointer;
}
.my-icon-menu:hover {
  color: #fff;
}
</style>
